package tommysmapeditor.helper.importer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

import tommysmapeditor.input.MapEditorInput;



//TODO refactor existing to work with that
public class JakoFormatImporter implements ImporterFactory {

	public MapEditorInput translate(String pathAndFileName) throws NumberFormatException, IOException {
		File mapFile = new File(pathAndFileName);
		String parentDir = mapFile.getParent();
		System.out.println("parent dir:"+ parentDir);

		
		
		
			FileReader fr = new FileReader(pathAndFileName);
			BufferedReader br = new BufferedReader(fr);
			String line = null;
			
			
			
			//JAKO tile size
			
			String readTileSize =br.readLine();
			System.out.println("tilesize: "+readTileSize);
			String readTileWidth =br.readLine();
			System.out.println("tileheight: "+readTileWidth);
			String readTileHeight =br.readLine();
			System.out.println("tileheight: "+readTileHeight);
			
			
			int tileSize = new Integer(readTileSize);
			int tileWidth= new Integer(readTileWidth);
			int tileHeight=new Integer(readTileHeight);

			MapEditorInput ret = 
				new MapEditorInput(
						tileSize,  
						tileWidth,  
						tileHeight
				);

			String readTileNumber =br.readLine();
			System.out.println("tilenumber: "+readTileNumber);

			int tileNumber=new Integer(readTileNumber);
			
			ret.initTilesArray(tileNumber);
			
			
			
			int lineCount = 0;
			while( lineCount <tileHeight ){
				line=br.readLine();
				System.out.println("line:"+line);
				String[] tiles = line.split(",");
				for(int k=0;k<tileWidth;k++){
					ret.tileGrid[k][lineCount]=Integer.valueOf(tiles[k]);
				}
				if(tiles.length==tileWidth){
					
				}else{
					System.out.println("warning: width not good in file");
				}
				lineCount++;
			}
			
			
			
			
			
			ret.EDITOR_WORK_TILES_PATH = parentDir;
			
			
			ret.tileFilePaths = new String[ret._tileNumber];
			for(int i=0;i<ret._tileNumber;i++){
				
				
				//JACKO ./maps/first/block1.bmp
				String tileFileNameWithShittyPath= br.readLine();
				
				String[] omgJako = tileFileNameWithShittyPath.split("/");
				
				String tileFileName = omgJako[omgJako.length-1];
				
				
				int remainingLngth = (tileFileNameWithShittyPath.length()-tileFileName.length());
				System.out.println("remaining length:"+remainingLngth);
				if(remainingLngth>0){
				ret.optionalTileNamePrefix = tileFileNameWithShittyPath.substring(0,remainingLngth-1 ) ;
				}
				System.out.println("optional prefix:"+ret.optionalTileNamePrefix);
				
				
				Image tile;
				
				//JAKO DIRTY HACK
				if( tileFileName.equals("-")){
					tile = new Image(Display.getCurrent(),tileSize,tileSize);
				}else{
					tile = new Image(Display.getCurrent(),ret.EDITOR_WORK_TILES_PATH+"/"+tileFileName);
				}
				
				ret.tileFilePaths[i]= tileFileName;
				
				
				final int dblwidth = tile.getBounds().width; 
				final int dblheight = tile.getBounds().height; 

				
				Image tileX2 =
					new Image(Display.getDefault(), 
							tile.getImageData()
								.scaledTo((int)(dblwidth * 2),(int)(dblheight * 2)));
				Image tileX3 = 
					new Image(Display.getDefault(), 
							tile.getImageData()
								.scaledTo((int)(dblwidth * 3),(int)(dblheight * 3)));
				ret.toDisposeOnSHutdown.add(tile);
				ret.toDisposeOnSHutdown.add(tileX2);
				ret.toDisposeOnSHutdown.add(tileX3);
				
				ret.tiles[0][i]=tile;
				ret.tiles[1][i]=tileX2;
				ret.tiles[2][i]=tileX3;
				
			}

			
		return ret;
	}

}
